package org.jeecg.modules.demo.hd.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.demo.hd.entity.AppUserGoldDetail;
import org.jeecg.modules.demo.hd.entity.AppUserShareDetail;
import org.jeecg.modules.demo.hd.mapper.AppUserShareDetailMapper;
import org.jeecg.modules.demo.hd.service.IAppUserShareDetailService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.time.LocalDateTime;
import java.util.List;

/**
 * @Description: app_user_share_detail
 * @Author: jeecg-boot
 * @Date: 2025-04-30
 * @Version: V1.0
 */
@Service
public class AppUserShareDetailServiceImpl extends ServiceImpl<AppUserShareDetailMapper, AppUserShareDetail> implements IAppUserShareDetailService {

    @Override
    public String findParentUser(String userId) {
        String parentUser = this.baseMapper.findParentUser(userId);
        return parentUser;
    }

    @Override
    public boolean exitParentUserInvitationRewards(String parentId, String userId) {
        LambdaQueryWrapper<AppUserShareDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(AppUserShareDetail::getUserId, parentId).eq(AppUserShareDetail::getData, userId);
        return this.baseMapper.selectCount(queryWrapper) > 0;
    }

    @Override
    public boolean exitCurrentDayUserDuiHuan() {
        // 查看当前用户在当天是否已经兑换过
        LoginUser currentUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        return exitCurrentDayUserDuiHuan(currentUser.getId());
    }

    @Override
    public boolean exitCurrentDayUserDuiHuan(String userId) {
        // 查看当前用户在当天是否已经兑换过
        LocalDateTime startOfDay = LocalDateTime.now().withHour(0).withMinute(0).withSecond(0).withNano(0);
        LocalDateTime endOfDay = LocalDateTime.now().withHour(23).withMinute(59).withSecond(59).withNano(999999999);
        LambdaQueryWrapper<AppUserShareDetail> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.between(AppUserShareDetail::getCreateTime, startOfDay, endOfDay);
//        queryWrapper.gt(AppUserGoldDetail::getSubsidyNum, 0);
        queryWrapper.eq(AppUserShareDetail::getUserId, userId);
        queryWrapper.eq(AppUserShareDetail::getShareType, "1");       // 金币兑换
        return this.baseMapper.selectCount(queryWrapper) > 0;

    }
}
